﻿Public Class IlacBarkodGiris
	Inherits Process

	Public Overrides Sub GenericEvent(ByVal e As ProcessEventArgs)
		Select Case e.EventType
			Case EventType.Initialize
				Start("İlaç Barkod Girişi", "Değişiklik yapmak için ilgili ilacı seçip Enter tuşuna basın.")
				AddList()
				List.AddColumns("kod(Kod;c;60),adi(İlaç adı;l;100%),diger(Diğer;l;50%),tur(Tür;l;150),dozaj(Dozaj;l;80),barkod(Barkod;c;50)")
				List.AddButton("muadil", "Muadil ilaçlar")
			Case EventType.Shown
				ShowList()
			Case EventType.Activate
				Dim Process As New IlacBarkodKayit
				Process.AddArgument("kod", List.SelectedRow("kod"))
				Window.Launch(Process, True)
			Case EventType.Other
				If e.Name = "List.muadil" Then
					Dim Process As New IlacBarkodMuadil
					Window.Launch(Process)
					'Process.Run(Window)
				End If
		End Select
	End Sub

	Sub ShowList()
		Rs = Saglik.Ilac.Liste(Remote)
		List.Clear()
		For Each Row In Rs
			Dim Ilac As New Ilac(Rs("ilactadi"))
			List.AddRow(Rs("ilactkod"), Rs("ilactadi"), Ilac.Diger, Ilac.Tur, Ilac.Dozaj, If(Rs("barkod") > 0, "Var", ""))
		Next
	End Sub

	Sub Kaydet(Barkod As String)
		Saglik.Ilac.BarkodKaydet(Remote, List.SelectedRow("kod"), Barkod, User.Kaykul)
		List.SelectedRows(0).Cells("barkod").Value = "Var"
	End Sub

End Class

Public Class IlacBarkodKayit
	Inherits Process

	Public Overrides Sub GenericEvent(ByVal e As ProcessEventArgs)
		Select Case e.EventType
			Case EventType.Initialize
				Start("İlaç Barkod Kaydı", "Kullanmak istediğiniz ilacı seçip kaydedin.")
				AddList("List", 500, 200)
				List.AddColumns("adi(İlaç adı;l;100%)")
			Case EventType.Shown
				Rs = Saglik.Ilac.Oku(Remote, Arguments("kod"))
				Dim Ilac As New Ilac(Rs("ilactadi"))
				Dim Adaylar As New List(Of Ilac)
				Rs = Saglik.Ilac.GucListe(Remote)
				For Each Row In Rs
					Dim Aday As New Ilac(Rs("adi"))
					Aday.Compare(Ilac)
					Adaylar.Add(Aday)
				Next
				Adaylar.Sort()
				For Each Item In Adaylar
					If Item.Puan > 0 Then
						List.AddRow(Item.Ad)
					End If
				Next
				Buttons.Show("Save")
			Case EventType.Save
				Dim kod = Remote.OpenSQL("select barkod from ssdmerkez..gucliste where adi = @adi", List.SelectedRow("adi"))(0)
				'CType(Caller, IlacBarkodGiris).Kaydet(kod)
				Close()
		End Select
	End Sub

End Class


Class Ilac : Implements IComparable(Of Ilac)

	Public Tur As String
	Public Ad As String
	Public Dozaj As String = ""
	Public Diger As String

	Sub New(Ad As String)
		Dim Birim = "MG.ML.GR.G.SUPP"
		Ad &= " "
		Me.Ad = Ad
		Ad = Ad.Replace("*", "")
		Ad = Ad.Replace(".", " ")
		Ad = Ad.Replace(",", " ")
		Ad = Ad.Replace("MGR", "MG")
		Ad = Ad.Replace("' S", "'S")
		Ad = Ad.Replace("  ", " ").Replace("  ", " ")
		For Each Item In Birim.Split(".")
			Ad = Ad.Replace(" " & Item & " ", Item & " ")
		Next
		Ad = Ad.Trim
		Dim Items = Split(Ad, " ")
		For i = 0 To Items.Length - 1
			If Items(i) <> "" Then
				Dim Test = CheckTur(Items(i))
				If Test <> "" Then
					Tur = Test
				Else
					If Items(i).EndsWith("'S") Then
						Tur = Items(i).Replace("'S", "") & " " & Tur
					ElseIf IsNumeric(Items(i)) AndAlso i < Items.Length - 1 Then
						Dim w = CheckTur(Items(i + 1))
						If w <> "" Then
							Tur = Items(i) & " " & w
							Items(i + 1) = ""
						End If
					Else
						Dim Found As Boolean = False
						For Each Item In Birim.Split(".")
							If Items(i).EndsWith(Item) AndAlso Len(Items(i)) > Len(Item) AndAlso Asc(Mid(Items(i), Len(Items(i)) - Len(Item), 1)) >= 48 AndAlso Asc(Mid(Items(i), Len(Items(i)) - Len(Item), 1)) <= 57 Then
								Found = True
								Dozaj = Items(i)
								Exit For
							End If
						Next
						If Not Found Then
							Diger &= " " & Items(i)
						End If
					End If
				End If
			End If
		Next
		Diger = Mid(Diger, 2)
	End Sub

	Public Puan As Integer

	Sub Compare(Aday As Ilac)
		Dim ld = 10 - LevenshteinDistance(Diger, Aday.Diger)
		If ld < 0 Then ld = 0
		Puan += ld * 10
		If Aday.Tur = Tur Then
			Puan += 50
		End If
		If Aday.Dozaj <> "" AndAlso Aday.Dozaj = Dozaj Then
			Puan += 30
		End If
	End Sub

	Public Function CompareTo(other As Ilac) As Integer Implements System.IComparable(Of Ilac).CompareTo
		If Puan > other.Puan Then Return -1
		If Puan < other.Puan Then Return 1
		If Puan = 0 Then Return 0
	End Function

	Shared Function LevenshteinDistance(s As String, t As String) As Double
		If s = "" Then Return 100
		If t = "" Then Return 100
		Dim m = s.Length
		Dim n = t.Length
		Dim d(m, n) As Integer

		For i = 0 To m
			d(i, 0) = i
		Next
		For j = 0 To n
			d(0, j) = j
		Next
		For j = 1 To n
			For i = 1 To m
				If s(i - 1) = t(j - 1) Then
					d(i, j) = d(i - 1, j - 1)
				Else
					d(i, j) = Math.Min(d(i - 1, j) + 1, Math.Min(d(i, j - 1) + 1, d(i - 1, j - 1) + 1))
				End If
			Next
		Next
		Return d(m, n)
	End Function

	Private Function CheckTur(x As String) As Object
		Select Case x
			Case "TAB", "TB", "TABLET" : Return "Tablet"
			Case "KREM", "CREM", "CRM", "CREAM", "KR" : Return "Krem"
			Case "SYR", "ŞURUP", "SRP", "SURUP", "SIR", "SİR", "SRP" : Return "Şurup"
			Case "POM", "POMAD", "POM." : Return "Pomat"
			Case "DAMLA", "DAM" : Return "Damla"
			Case "DRJ", "DRJ." : Return "Draje"
			Case "AMP" : Return "Ampul"
			Case "SPRAY", "SPREY", "SPR" : Return "Sprey"
			Case "CAP", "CAPS", "KAP" : Return "Kapsül"
			Case "GARGARA" : Return "Gargara"
			Case "GEL", "JEL" : Return "Jel"
			Case "LOSYON" : Return "Losyon"
			Case "SUSP", "SÜSP", "SUS" : Return "Süspansiyon"
			Case "FLK" : Return "Flakon"
			Case "GRANUL", "GRANÜL", "GRANÜLE" : Return "Granül"
			Case "SERUM" : Return "Serum"
			Case "SOLÜSYON", "SOL" : Return "Solüsyon"
			Case "İNH", "INH" : Return "Inhaler"
			Case "SUPP" : Return "Fitil"
			Case "MER" : Return "Merhem"
			Case "ENJ" : Return "Enjeksiyon"
		End Select
		Return ""
	End Function

End Class

Public Class IlacBarkodMuadil
	Inherits Process

	Public Overrides Sub GenericEvent(ByVal e As ProcessEventArgs)
		Select Case e.EventType
			Case EventType.Initialize
				Start("Muadil Barkod Listesi", "")
				AddList()
				List.AddColumns("^check(;c;25),barkod1(Barkod 1;c;100),ilac1(İlaç adı;l;100%),jenerik(Jenerik;l;100%),ilac2(İlaç adı;l;100%),tur(Tür;l;100),dozaj(Dozaj;l;80),barkod2(Barkod 2;c;100)")
			Case EventType.Shown
				ShowList()
				Buttons.Show("Save")
			Case EventType.Save
				If Not Confirm("Kaydetmek istediğinizden emin misiniz?") Then Return
				Dim L As New List(Of String)
				For Each Row As DataGridViewRow In List.Rows
					If Row.Cells("check").Value Then
						L.Add(Row.Cells("barkod1").Value & "." & Row.Cells("barkod2").Value)
					End If
				Next
				Saglik.Ilac.MuadilKaydet(Remote, L)
				Info("Muadil ilaçlar kaydedilmiştir.")
				Close()
		End Select
	End Sub

	Sub ShowList()
		Rs = Saglik.Ilac.GucListeMuadil(Remote)
		List.Clear()
		For Each Row In Rs
			Dim Ilac1 As New Ilac(Rs("ilac1"))
			Dim Ilac2 As New Ilac(Rs("ilac2"))
			If Ilac1.Dozaj = Ilac2.Dozaj And Ilac1.Tur = Ilac2.Tur And Ilac1.Tur <> "" And Ilac1.Dozaj <> "" Then
				List.AddRow(True, Rs("barkod1"), Rs("ilac1"), Rs("jenerik"), Rs("ilac2"), Ilac1.Tur, Ilac1.Dozaj, Rs("barkod2"))
			End If
		Next
	End Sub

End Class